﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLUProject.Domain;
using DLUProject.Services;
using ColorLife.Core.FileManager;
using ColorLife.Core.Helper;
public partial class TaiKhoan_Install_Install : BaseUserControl
{
    public IServices<Systems> _systemService { get; set; }
    public IServices<WorkGroup> _wkService { get; set; }
    public IServices<Function> _funcService { get; set; }
    public WorkGroupExtension _workGroupEx { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    void LoadParentID(int parentId, DropDownList ddl, List<WorkGroup> list)
    {

        ddl.Items.Clear();
        ddl.Items.Add(new ListItem("-- Chọn mục cha --", "0"));
        foreach (var item in list)
        {
            ddl.Items.Add(new ListItem(item.Name, item.WorkGroupID.ToString()));
        }
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        if (!IsPostBack)
        {
            foreach (var item in _systemService.Table)
            {
                SystemID.Items.Add(new ListItem(item.Name, item.SystemID.ToString()));
                ddlSystemID.Items.Add(new ListItem(item.Name, item.SystemID.ToString()));
            }
        }
    }
    protected void btnSave_ServerClick(object sender, EventArgs e)
    {
        var folderSys = _systemService.Get(SystemID.SelectedValue.ToInt());
        if (folderSys == null)
            Response.Redirect("Default.aspx");

        IFileRepository fileResp = new FileRepository();

        var listDir = fileResp.All(folderSys.Url, FileFilter.Directories);
        foreach (var item in listDir)
        {
            var wk = new WorkGroup
            {
                SystemID = SystemID.SelectedValue.ToInt(),
                ParentID = 0,
                Url = "/" + item.Name,

                Name = item.Name,
                Image = "/Content/icons/chart_pie.png",
            };
            _wkService.Insert(wk);
        }
        //  StatusBox.InnerHtml = "Cập nhật dữ liệu thành công.";
        //   StatusBox.Attributes.Add("class", "alert alert-info");
        StatusBox.Visible = true;
    }
    protected void ddlSystemID_SelectedIndexChanged(object sender, EventArgs e)
    {
        int systemId = ddlSystemID.SelectedValue.ToInt();
        var list = _workGroupEx.GetByParent(new List<WorkGroup>(), 0).Where(c => c.SystemID == systemId).ToList();
        LoadParentID(0, WorkGroupID, list);
    }
    protected void btnSaveFunc_ServerClick(object sender, EventArgs e)
    {
        int wkId = WorkGroupID.SelectedValue.ToInt();
        var workGroup = _wkService.All().SingleOrDefault(c => c.WorkGroupID == wkId);
        if (workGroup == null)
            Response.Redirect("Default.aspx");
            
        IFileRepository file = new FileRepository();
        var listFile = file.All("/Portal" + workGroup.UrlFull, FileFilter.Files, "^.ascx$");
        foreach (var item in listFile)
        {
            var wk = new Function
            {
                WorkGroupID = WorkGroupID.SelectedValue.ToInt(),

                Url = item.Name.Substring(0, item.Name.LastIndexOf(".ascx")),

                Name = item.Name.Substring(0, item.Name.LastIndexOf(".ascx"))
            };
            _funcService.Insert(wk);

          //  Label1.Text += item.FullName;
        }

        //  StatusBox.InnerHtml = "Cập nhật dữ liệu thành công.";
        //   StatusBox.Attributes.Add("class", "alert alert-info");
       // btnSaveFunc.Text = listFile.Count.ToString();
        StatusBox.Visible = true;
    }
}